/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pio.action.administrador;

import DBMS.DataBaseManager;
import clases.Turno;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

/**
 *
 * @author tamerdark
 */
public class TurnosAction extends DispatchAction{
    
    public ActionForward add(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {        
        Turno t = (Turno)form;
        if(t.hasValidAtts()){
            DataBaseManager dbms = DataBaseManager.getInstance();
            if(dbms.agregarTurno(t)){
                readAll(mapping,form,request,response);
                t.setMensaje("Turno agregado exitosamente.");
                return mapping.findForward("add.success");
            }else{
                t.setMensaje("Error al agregar turno. Intente mas tarde.");
                return mapping.findForward("add.failure");
            }
        }else{
            t.setMensaje("Errores en atributos del turno ingresado.");
            return mapping.findForward("add.failure");
        }
    }
    
    public ActionForward update(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        Turno t = (Turno)form;
        if(t.hasValidAtts()){
            DataBaseManager dbms = DataBaseManager.getInstance();
            if(dbms.modificarTurno(t)){
                readAll(mapping,form,request,response);
                t.setMensaje("Turno actualizado exitosamente.");
                return mapping.findForward("update.success");
            }else{
                t.setMensaje("Error al modificar turno. Intente mas tarde.");
                return mapping.findForward("update.failure");
            }
        }else{
                t.setMensaje("Error en atributos del turno modificado.");
            return mapping.findForward("update.failure");
        }
    }
    
    public ActionForward readAll(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        DataBaseManager dbms = DataBaseManager.getInstance();
        List<Turno> l = dbms.consultarTurno(new Turno());
        request.setAttribute("turnos", l);        
        return mapping.findForward("readAll.success");
    }
    
    public ActionForward delete(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        Turno t = (Turno)form;
        DataBaseManager dbms = DataBaseManager.getInstance();
        if(dbms.eliminarTurno(t)){
            readAll(mapping,form,request,response);
                t.setMensaje("Turno eliminado exitosamente.");
            return mapping.findForward("delete.success");
        }else{
                t.setMensaje("Error al eliminar turno. Intente mas tarde.");
            return mapping.findForward("delete.failure");
        }
    }
    
}
